package com.example.service;

import cn.hutool.core.date.DateUtil;
import com.example.entity.Article;
import com.example.mapper.ArticleMapper;
import com.example.mapper.CollectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private CollectMapper collectMapper;

    /**
     * 新增
     */
    public void add(Article article) {
        article.setReadCount(0);//旅游攻略的阅读量的初始值设为0
        article.setDate(DateUtil.today());//设置发布日期
        articleMapper.insert(article);
    }

    /**
     * 更新
     */
    public void updateById(Article article) {
        articleMapper.updateById(article);
    }

    /**
     * 删除
     */
    public void deleteByid(Integer id) {
        articleMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
/*        for (Integer id : ids) {
            articleMapper.deleteById(id);
        }*/
        articleMapper.deleteBatch(ids);
    }

    /**
     * 根据id查询
     */
    public Article selectById(Integer id) {
        Article article = articleMapper.selectById(id);
        Integer collectCount = collectMapper.selectCountByFid(article.getId()); //查询当前攻略的收藏量
        article.setCollectCount(collectCount); //设置收藏量
        return article;
    }

    /**
     * 查询所有（条件查询）
     */
    public List<Article> selectAll(Article article) {
        return articleMapper.selectAll(article);
    }

    /**
     * 分页查询（在查询所有的基础上进行分页）
     */
    public PageInfo<Article> selectPage(Article article,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Article> list = articleMapper.selectAll(article);
        return PageInfo.of(list);
    }

    /**
     * 查询推荐的旅游攻略
     */
    public List<Article> selectRecommend() {
        return articleMapper.selectRecommend();
    }
}
